package com.syy.spider.controller;

import com.syy.spider.pojo.User;
import com.syy.spider.service.LoginService;
import com.syy.spider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    @RequestMapping("/loginHtml")
    public String loginHtml(){
        return "login/login";
    }

    @RequestMapping("/adminLoginHtml")
    public String adminLoginHtml(){
        return "login/adminLogin";
    }

    @RequestMapping("/registerHtml")
    public String registerHtml(){
        return "login/register";
    }

    @RequestMapping(value = "/login",method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView login(HttpServletRequest request, HttpSession session){
        ModelAndView mav = new ModelAndView();
        User user = new User();
        String username = request.getParameter("username");
        System.out.println("用户名:"+username);
        user.setUsername(username);
        String password = request.getParameter("password");
        user.setPassword(password);
        boolean loginResult = loginService.login(user);
        if (loginResult){
            mav.setViewName("pages/notice");
            session.setAttribute("user",user);
        }else {
            mav.setViewName("login/loginFail");
        }
        return mav;
    }

    @RequestMapping(value = "/register",method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView regist(HttpServletRequest request, HttpSession session){
        ModelAndView mav = new ModelAndView();
        User user = new User();
        String username = request.getParameter("username");
        user.setUsername(username);
        String password = request.getParameter("password");
        user.setPassword(password);
        boolean registResult = loginService.regist(user);
        if (registResult){
            mav.setViewName("login/login");
        }else {
            mav.setViewName("login/registFail");
        }
        return mav;
    }

    @RequestMapping("exit")
    public String exit(HttpServletRequest request, HttpSession session){
        session.removeAttribute("user");
        return "pages/index";
    }

    @PostMapping("/changePassword")
    @ResponseBody
    public int changePassword(HttpServletRequest request, HttpSession session,@RequestBody Map<String, Object> params){
        System.out.println("进入修改密码方法");
        String oldPas =(String) params.get("oldPas");
        System.out.println("旧密码"+oldPas);
        String newPas =(String) params.get("newPas");
        System.out.println("新密码"+newPas);
        User user = (User) request.getSession().getAttribute("user");
        String username = user.getUsername();
        System.out.println("用户名"+username);
        User getUser = userService.selectUserByNameAndPas(oldPas,username);
        if (getUser!=null){
            if (!oldPas.equals(newPas)){
                userService.updatePwd(username,newPas);
                return 1;
            }else {
                return 2;
            }
        }else {
            return 3;
        }
    }
}
